package com.stars.home.vmodel

import android.app.Activity
import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.stars.home.activity.*
import com.stars.home.config.ConfigRx
import com.stars.home.config.MIntentAction
import com.stars.home.model.User
import com.stars.home.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch


/**
 *
 *on 2020/9/23
 */
open class HomeMeVModel(application: Application) : BViewModel<BRepository>(application) {

    var  cacheSize= MutableLiveData<String>()
    var user=MutableLiveData<User>()

    var isOnline=MutableLiveData<Boolean>(false)

    var isNoMsg=MutableLiveData<Boolean>(false)
    //工作经验
    var workTime=MutableLiveData<String>()




    //个人信息
    fun toPersonal(){

        UiSwitch.single(repository().context, PersonalInfoActivity::class.java)
    }

    //服务设置
    fun toSetUp(){

        UiSwitch.single(repository().context, SetUpActivity::class.java)

    }

    //账单
    fun toBill(){
        UiSwitch.single(repository().context, BillActivity::class.java)

    }

    //消息
    fun toMsg(){
        UiSwitch.single(repository().context, MsgActivity::class.java)

    }
    //关于我们
    fun toAboutUs(){
        UiSwitch.single(repository().context, AboutActivity::class.java)


//        UiSwitch.bundle(repository().context, ArticleDetailActivity::class.java,B().putString("type","1").ok())

    }

    //实名认证
    fun toCertion(){

        if(user.value?.type==0&&user.value?.status==0){
            //未认证

            UiSwitch.bundle(repository().context, CertificationActivity::class.java, B().putString("type","2").ok())


        }else{
            //认证中、认证失败
            UiSwitch.single(repository().context, RealNameActivity::class.java)

        }


    }

    //设置
    fun toSetting(){
        UiSwitch.single(repository().context, SettingActivity::class.java)

    }

    //意见反馈
    fun toOpin(){

        UiSwitch.single(repository().context, OpinionActivity::class.java)

    }

    //余额
    fun toBalance(){

        UiSwitch.single(repository().context,  SurplusActivity::class.java)

        //充值
     //   UiSwitch.single(repository().context, BalanceActivity::class.java)

    }

    fun getUserInfo(){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.getUser() }
            if(it.ok()){
                user.value=it.data
                isOnline.value = user.value?.isOnline==1
                if(null!=it.data?.workTime){
                    workTime.value=it.data?.workTime

                }else{
                    workTime.value="0"
                }

                view().refreshing(false)
            }else{
                view().refreshing(false)
            }
        }
    }

    fun getNotMsg(){
        viewModelScope.launch{
            var it=repository().lreq { NetEngine.service.notReadNumber() }
            if(it.ok()){
                var num=it.data
                isNoMsg.value = num!!>0

                getUserInfo()
            }
        }
    }


    fun setStatus(){

        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.updateOnline() }
            if(it.ok(true)){
                isOnline.value=!isOnline.value!!



            }
        }

    }

    //连续上传位置
    fun toUpdateLocation(lat: String, lng: String){
        viewModelScope.launch {
            var it= NetEngine.service.updateTechnicianPosition(lat,lng)
            if(it.ok(true)){

            }
        }
    }

}
